import { getUserInfoAPI, loginAPI } from '@/api/user'
import router from '@/router'
import { getToken, setToken } from '@/utils/auth'
import { Message } from 'element-ui'

const state = {
  token: getToken(),
  userInfo: {},
  routes: []
}

const mutations = {
  saveRoutes(state, payload) {
    state.routes = payload
  },
  saveToken(state, payload) {
    state.token = payload
  },
  saveUseInfo(state, payload) {
    state.userInfo = payload
  }
}

const actions = {
  async loginRequest({ commit }, payload) {
    const res = await loginAPI(payload)
    setToken(res.data)
    commit('saveToken', res.data)
    Message.success('登录成功')
    router.push('/')
  },
  async getUserInfoAction({ commit }) {
    const res = await getUserInfoAPI()
    // console.log(res)
    commit('saveUseInfo', res.data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

